<?php
namespace app\api\controller;

use think\Request;
use think\Controller;

/**
 * 功能说明:
 * Company:物联网应用专家---成都亿佰特电子科技有限公司
 * User: 郑文军
 * Time: 2021-09-30 10:48
 * version：1.0
 */
class Base extends Controller
{



    protected $accessKey;       # 入口KEY值
    protected $adminSign;       # 账号校验KEY值
    protected $accountId;       # 客户ID
    protected $account;         # 客户账号
    protected $page;            # 分页参数
    protected $timeStamp;       # 当前时间戳
    protected $url;       # 当前时间戳
    protected $accessData;

    public function _initialize()
    {

        header('Access-Control-Allow-Origin: *');
        $this->page = 30;
        $this->timeStamp = time();
        //Options请求直接返回空
        if (request()->isOptions()) {
            return "";
            exit;
        }

        // 控制器名
        $controller = request()->controller();

        // 获取方法名
        $action = request()->action();

        $no_verify_func = ['user/login','user/register'];


        $this->accessKey = config('custom.accessKey');
        $param = Request::instance()->header('authorization');
        $mstr = str_replace('Bearer ', '', $param);
        $data = [];
        $data['action'] = $action;
        $data['controller'] = $controller;
        $data['no_verify_func'] = $no_verify_func;
        $data['access_key'] = $this->accessKey;
        $data['mstr'] = $mstr;
        // 入口验证
        $this->accessData = accessVerify($data);
    }



    /**
     * $return array/string
     * @param $url 需要修改域名地址的数组或者字符串
     * @param string $title 名称
     * @param bool $time 是否图片链接加随机时间字符,避免cdn缓存
     * @param bool $https 是否将http转换为https
     * @return array|mixed|string
     * @功能说明: 批量给图片加域名,自动将http转换为https
     * @author 郑
     * @DataTime: 2020/7/3 16:31
     */
    public function addHttps($url, $title = '', $time = false, $https = false)
    {
        if (!is_string($url)) {
            $list = [];
            foreach ($url as $v) {
                $v[$title] = is_int(strpos($v[$title], 'http')) ? $v[$title] : $this->url . $v[$title];
                $v[$title] = $time ? $v[$title] . '?time=' . time() : $v[$title];
                if ($https) {
                    $v[$title] = is_int(strpos($v[$title], 'https')) ? $v[$title] : str_replace("http", "https", $v[$title]);
                }
                //反斜杠转义
                $v[$title] = str_replace('\\', '/', $v[$title]);
                $list[] = $v;
            }
            return $list;
        } else {
            $url = is_int(strpos($url, 'http')) ? $url : $this->url . $url;
            $url = $time ? $url . '?time=' . time() : $url;
            if ($https) {
                $url = is_int(strpos($url, 'https')) ? $url : str_replace("http", "https", $url);
            }
            $url = str_replace('\\', '/', $url);
            $url = ltrim($url, '/');
            return $url;
        }
    }
}